package com.kurumi.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kurumi.config.MyConfig;
import com.kurumi.domain.PractitionerVO;
import com.kurumi.mapper.BaseInfoMapper;
import com.kurumi.mapper.MedicalRecordBorrowApplyItemMapper;
import com.kurumi.mapper.MedicalRecordBorrowApplyMapper;
import com.kurumi.mapper.MedicalRecordImageMapper;
import com.kurumi.mapper.MedicalRecordMapper;
import com.kurumi.mapper.MedicalRecordPrinterApplyItemMapper;
import com.kurumi.mapper.MedicalRecordPrinterApplyMapper;
import com.kurumi.mapper.MedicalRecordQualityControlMapper;
import com.kurumi.mapper.MedicalRecordTraceMapper;
import com.kurumi.mapper.homepage.HomepagePdfMapper;
import com.kurumi.mapper.homepage.MedicalRecordBasicMapper;
import com.kurumi.mapper.homepage.MedicalRecordCostMapper;
import com.kurumi.mapper.homepage.MedicalRecordDiagnossMapper;
import com.kurumi.mapper.homepage.MedicalRecordDoctorMapper;
import com.kurumi.mapper.homepage.MedicalRecordIcuMapper;
import com.kurumi.mapper.homepage.MedicalRecordNurseMapper;
import com.kurumi.mapper.homepage.MedicalRecordOperationMapper;
import com.kurumi.mapper.homepage.MedicalRecordTransfusionMapper;
import com.kurumi.mapper.homepage.MedicalRecordTurnMapper;
import com.kurumi.pojo.homepage.HomepagePdf;
import com.kurumi.pojo.homepage.MedicalRecordBasic;
import com.kurumi.pojo.homepage.MedicalRecordCost;
import com.kurumi.pojo.homepage.MedicalRecordDiagnoss;
import com.kurumi.pojo.homepage.MedicalRecordDoctor;
import com.kurumi.pojo.homepage.MedicalRecordIcu;
import com.kurumi.pojo.homepage.MedicalRecordNurse;
import com.kurumi.pojo.homepage.MedicalRecordOperation;
import com.kurumi.pojo.homepage.MedicalRecordTransfusion;
import com.kurumi.pojo.homepage.MedicalRecordTurn;
import com.kurumi.query.MedicalRecordQuery;
import com.kurumi.service.MedicalRecordDataManageService;
import com.kurumi.util.FileUtil;
import com.kurumi.util.JsonUtil;
import com.kurumi.util.StringUtil;

@Transactional
@Service
public class MedicalRecordDataManageServiceImpl implements MedicalRecordDataManageService {

	@Autowired
	private MyConfig myConfig;
	
	@Autowired
	private MedicalRecordMapper medicalRecordMapper;
	
	@Autowired
	private MedicalRecordTraceMapper medicalRecordTraceMapper;
	
	@Autowired
	private MedicalRecordBorrowApplyMapper borrowMapper;
	
	@Autowired
	private MedicalRecordBorrowApplyItemMapper borrowItemMapper;
	
	@Autowired
	private MedicalRecordPrinterApplyMapper printMapper;
	
	@Autowired
	private MedicalRecordPrinterApplyItemMapper printItemMapper;
	
	@Autowired
	private MedicalRecordQualityControlMapper qualityControlMapper;
	
	@Autowired
	private MedicalRecordImageMapper medicalRecordImageMapper;
	
	@Autowired
	private MedicalRecordTurnMapper medicalRecordTurnMapper;
	
	
	@Autowired
	private BaseInfoMapper baseInfoMapper;
	
	@Autowired
	private MedicalRecordBasicMapper medicalRecordBasicMapper;
	
	
	@Autowired
	private MedicalRecordDiagnossMapper medicalRecordDiagnossMapper;
	
	@Autowired
	private MedicalRecordNurseMapper medicalRecordNurseMapper;
	
	@Autowired
	private MedicalRecordOperationMapper medicalRecordOperationMapper;
	
	@Autowired
	private MedicalRecordCostMapper medicalRecordCostMapper;
	
	@Autowired
	private MedicalRecordTransfusionMapper medicalRecordTransfusionMapper;
	
	@Autowired
	private MedicalRecordIcuMapper medicalRecordIcuMapper;
	
	@Autowired
	private MedicalRecordDoctorMapper medicalRecordDoctorMapper;
	
	@Autowired
	private HomepagePdfMapper homepagePdfMapper;
 	
	
	@Override
	public List<Map<String,Object>> getMedicalRecordOfExport(MedicalRecordQuery medicalRecordQuery) {
		// TODO Auto-generated method stub
		return medicalRecordMapper.getMedicalRecordOfExport(medicalRecordQuery);
	}

	@Override
	public List<Map<String, Object>> getMedicalRecordOfExcel(MedicalRecordQuery medicalRecordQuery) {
		List<String> datas = medicalRecordMapper.getMedicalRecordJsonByMedicalRecordQuery(medicalRecordQuery);
		List<Map<String,Object>> records = new ArrayList<Map<String,Object>>();
		for (String data : datas) {
			if(data != null){
				Map<String, Object> jsonMap = JsonUtil.jsonToPojo(data, Map.class);
				records.add(jsonMap);
			}
		}
		return records;
		/*List<Map<String,Object>> medicalRecords = medicalRecordMapper.getMedicalRecordOfExport(medicalRecordQuery);
		for (Map<String, Object> medicalRecord : medicalRecords) {
			String visitGuid = (String)medicalRecord.get("visit_guid");
			List<String> jsonDatas = medicalRecordMapper.getMedicalRecordJsonByVisitGuid(StringUtil.handleJsonParam(visitGuid));
			if(!jsonDatas.isEmpty()){
				Map<String, Object> jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
				medicalRecord.put("jsonMap", jsonMap);
			}
		}
		// TODO Auto-generated method stub
		return medicalRecords;*/
	}

	@Override
	public int medicalRecordDelete(String visitGuid) {
		// TODO Auto-generated method stub
		medicalRecordTraceMapper.deleteMedicalRecordPageTrace(visitGuid);
		medicalRecordTraceMapper.deleteMedicalRecordTrace(visitGuid);
		List<Integer> borrowIds = borrowMapper.getIdsOfMedicalRecordBorrow(visitGuid);
		for (Integer borrowId : borrowIds) {
			borrowItemMapper.deleteBorrowItemByBorrowId(borrowId);
			borrowMapper.deleteBorrowByBorrowId(borrowId);
		}
		List<Integer> pageBorrowIds = borrowMapper.getIdsOfMedicalRecordPageBorrow(visitGuid);
		for (Integer pageBorrowId : pageBorrowIds) {
			borrowItemMapper.deletePageBorrowItemByPageBorrowId(pageBorrowId);
			borrowMapper.deletePageBorrowByPageBorrowId(pageBorrowId);
		}
		
		List<Integer> printIds = printMapper.getIdsOfMedicalRecordPrint(visitGuid);
		for (Integer printId : printIds) {
			printItemMapper.deletePrintItemByPrintId(printId);
			printMapper.deleteByPrimaryKey(printId);
		}
		List<Integer> qualityControlIds = qualityControlMapper.getIdsOfMedicalRecordQualityControl(visitGuid);
		for (Integer qualityControlId : qualityControlIds) {
			qualityControlMapper.deleteQualityControlItemByQualityControlId(qualityControlId);
			qualityControlMapper.deleteQualityControlByQualityControlId(qualityControlId);
		}
		medicalRecordImageMapper.deleteMedicalRecordImageByVisitGuid(visitGuid);
		medicalRecordMapper.deleteMedicalRecordJsonByVisitGuid(StringUtil.handleJsonParam(visitGuid));
		medicalRecordMapper.deleteByPrimaryKey(visitGuid);
		String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
		FileUtil.deleteFile(new File(filePath));
		return 1;
	}

	
	@Override
	public List<String> getMediclJson(String startTime, String endTime) {
		// TODO Auto-generated method stub
		return medicalRecordMapper.getMedicalRecordJsonByOutTime(startTime,endTime);
	}

	@Override
	public Map<String, Object> getMedicalRecordMapByGuid(String guid) {
		String json = medicalRecordMapper.getMedicalRecordJsonByGuid(guid);
		if(StringUtils.isNotBlank(json)){
			return JsonUtil.jsonToPojo(json, Map.class);
		}
		return null;
	}

	@Override
	public String getRegionCodeByName(String regionName) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getRegionNameByCode(regionName);
	}
	
	
	@Override
	public List<String> getMedicalRecordJsonByPage(String startTime, String endTime, int startIndex, int endIndex) {
		// TODO Auto-generated method stub
		return medicalRecordMapper.getMedicalRecordJsonByPage(startTime, endTime, startIndex, endIndex);
	}

	@Override
	public String getMedicalRecordJsonByGuid(String guid) {
		List<String> jsonList = medicalRecordMapper.getMedicalRecordJsonByVisitGuid("\""+guid+"\"");
		if(CollectionUtils.isNotEmpty(jsonList)) {
			return jsonList.get(0);
		}
		return null;
	}

	@Override
	public String getCertificatePracticeByName(String name, int type) {
		return baseInfoMapper.getCertificatePracticeByName(name, type);
	}

	/**
	 * 根据工号查询执业人员信息
	 * @param code 人员工号
	 */
	@Override
	public PractitionerVO getCertificateByMedicalWorkCode(String code) {
		return baseInfoMapper.getCertificateByMedicalWorkCode(code);
	}

	/**
	 * 根据国籍字典名称查询国籍英文名称
	 * @param nationalityName 国籍名称
	 * @return
	 */
	@Override
	public String getEnnameByName(String nationalityName) {
		return baseInfoMapper.getEnnameByName(nationalityName);
	}

	/**
	 * 保存或更新病案基本信息
	 */
	@Override
	public void saveOrUpdateMedicalRecordBasic(MedicalRecordBasic basic) {
		String visitGuid = basic.getVisitGuid();
		// 先删除再新增
		medicalRecordBasicMapper.deleteByVisitGuid(visitGuid);
		// 保存
		System.out.println(basic.getBabyBirthWeight());
		medicalRecordBasicMapper.insertSelective(basic);
	}

	@Override
	public void saveOrUpdateMedicalRecordDiagnoss(List<MedicalRecordDiagnoss> list,String visitGuid) {
		// 先根据guid删除 
		medicalRecordDiagnossMapper.deleteByVisitGuid(visitGuid);
		// 保存
		for(MedicalRecordDiagnoss medicalRecordDiagnoss : list) {
			medicalRecordDiagnossMapper.insertSelective(medicalRecordDiagnoss);
		}
		
	}

	/**
	 * 保存或更新护理信息
	 */
	@Override
	public void saveOrUpdateNurse(MedicalRecordNurse nurse) {
		String visitGuid = nurse.getVisitGuid();
		// 先根据visitGuid删除
		medicalRecordNurseMapper.deleteByVisitGuid(visitGuid);
		
		medicalRecordNurseMapper.insertSelective(nurse);
		
	}

	// 保存或更新手术信息
	@Override
	public void saveOrUpdateOperation(List<MedicalRecordOperation> operationList, String visitGuid) {
		// 根据guid删除手术信息
		medicalRecordOperationMapper.deleteByVisitGuid(visitGuid);
		// 添加手术
		for (MedicalRecordOperation medicalRecordOperation : operationList) {
			medicalRecordOperationMapper.insertSelective(medicalRecordOperation);
		}
		
	}
	
	/**
	 * 保存或更新费用信息
	 */
	@Override
	public void saveOrUpdateCost(MedicalRecordCost cost) {
		// 根据guid删除费用信息
		medicalRecordCostMapper.deleteByVisitGuid(cost.getVisitGuid());
		// 保存费用信息
		medicalRecordCostMapper.insertSelective(cost);
		
	}

	@Override
	public void saveOrUpdateTransfusion(List<MedicalRecordTransfusion> list, String visitGuid) {
		// 根据guid删除输血信息
		medicalRecordTransfusionMapper.deleteByVisitGuid(visitGuid);
		
		// 保存输血信息
		for (MedicalRecordTransfusion medicalRecordTransfusion : list) {
			medicalRecordTransfusionMapper.insertSelective(medicalRecordTransfusion);
		}
	}

	/**
	 * 保存或更新医师信息
	 */
	@Override
	public void saveOrUpdateDoctor(List<MedicalRecordDoctor> doctorList, String visitGuid) {
		// 根据guid删除医师信息
		medicalRecordDoctorMapper.deleteByVisitGuid(visitGuid);
		//保存医师信息
		for (MedicalRecordDoctor medicalRecordDoctor : doctorList) {
			medicalRecordDoctorMapper.insertSelective(medicalRecordDoctor);
		}
	}

	/**
	 * 保存或更新icu信息
	 */
	@Override
	public void saveOrUpdateICU(List<MedicalRecordIcu> icuList, String visitGuid) {
		// 根据guid删除icu信息
		medicalRecordIcuMapper.deleteByVisitGuid(visitGuid);
		// 保存ICU信息
		for (MedicalRecordIcu medicalRecordIcu : icuList) {
			medicalRecordIcuMapper.insertSelective(medicalRecordIcu);
		}
	}

	@Override
	public void saveOrUpdateTurn(List<MedicalRecordTurn> turnList, String visitGuid) {
		// 根据visitGuid删除
		medicalRecordTurnMapper.deleteByVisitGuid(visitGuid);
		// 保存转科信息
		for (MedicalRecordTurn medicalRecordTurn : turnList) {
			medicalRecordTurnMapper.insertSelective(medicalRecordTurn);
		}
	}

	
	/**
	 * 根据唯一标识查询pdf记录信息
	 * @param onlyId 唯一标识
	 * @return pdf记录信息
	 */
	@Override
	public HomepagePdf getHomepagePdfByOnlyId(String onlyId) {
		return homepagePdfMapper.getHomepagePdfByOnlyId(onlyId);
	}

	@Override
	public void saveOrUpdateHomepagePdf(HomepagePdf homepagePdf) {
		Integer id = homepagePdf.getId();
		if(id==null) {
			homepagePdfMapper.insert(homepagePdf);
		}else {
			homepagePdfMapper.updateByPrimaryKeySelective(homepagePdf);
		}
	}

	/**
	 * 获取未同步PDF数据
	 * @param homepagePdf
	 */
	@Override
	public List<HomepagePdf> getPDFWaitingforSyncToNeusoft() {
		// TODO Auto-generated method stub
		return homepagePdfMapper.getPDFWaitingforSyncToNeusoft();
	}

	/**
	 * 更新首页pdf信息为已更新
	 * @param homepagePdf
	 */
	@Override
	public void updatePdfIsSyncStatus(Integer id) {
		// TODO Auto-generated method stub
		homepagePdfMapper.updatePdfIsSyncStatus(id);
	}
	
	/**
	 * 同步medical_record_basic缺少的数据信息
	 * @param startTime 出院查询开始时间
	 * @param string2  出院查询结束时间
	 * @return
	 */
	@Override
	public List<String> syncMediclRecordBasicVisitGuid(String startTime, String endTime){
		return medicalRecordBasicMapper.syncMediclRecordBasicVisitGuid(startTime,endTime);
	}
	
	/**
	 * 根据唯一标识查询guid集合
	 * @param onlyIds
	 * @return
	 */
	public List<String> getVisitGuidByOnlyIds(String onlyIds){
		return medicalRecordBasicMapper.getVisitGuidByOnlyIdList(Arrays.asList(onlyIds.split(",")));
	}


}
